import os
from utils import doc_util, read_config
from service import get_a_stock_info

common_path = os.path.join(read_config.data_path, 'common')

stock_dict = doc_util.get_a_doc_dict(os.path.join(common_path, 'config', '0.股票信息'), 'code')
macd_dict = doc_util.get_a_doc_dict(os.path.join(common_path, 'macd', '000000'), 'code')


# 获取股票基本信息
def stock(code):
    global stock_dict
    if code not in stock_dict.keys():
        stock_dict = doc_util.get_a_doc_dict(os.path.join(common_path, 'config', '0.股票信息'), 'code')
    if code not in stock_dict.keys():
        stock_dict = get_a_stock_info.add_a_stock(code)
        doc_util.gen_a_doc(os.path.join(common_path, 'config'), '0.股票信息', stock_dict)
    return stock_dict[code]


# 获取股票基本信息
def get_stock_cache():
    global stock_dict
    if len(stock_dict) == 0:
        stock_dict = doc_util.get_a_doc_dict(os.path.join(common_path, 'config', '0.股票信息'), 'code')
    return stock_dict


# 获取股票基本信息
def new_stock_all():
    global stock_dict
    if len(stock_dict) == 0:
        stock_dict = doc_util.get_a_doc_dict(os.path.join(common_path, 'config', '0.股票信息'), 'code')
    result = {}
    for key in stock_dict.keys():
        result[key] = stock_dict[key]
    return result


# 每天刷新
def macd(code):
    global macd_dict
    if code not in macd_dict.keys():
        macd_dict = doc_util.get_a_doc_dict(os.path.join(common_path, 'macd', '000000'), 'code')
    if code not in macd_dict.keys():
        return []
    return macd_dict[code]


if __name__ == '__main__':
    s_dict = stock('601919')
    print(s_dict)
